<?php
//分页类

class Page
{
    public $page = 1;      //当前页
    public $pageSize=10;   //页大小（每页数据条数）
    public $maxRows;       //最多条数
    public $maxPage;       //最多页数
    
    public function __construct($maxRows,$pageSize=10)
    {
        $this->maxRows = $maxRows;
        $this->pageSize = $pageSize;
        $this->page = empty($_GET['p'])?1:$_GET['p']; //尝试获取当前页
        $this->mkMaxPage();
        $this->checkPage();
        
    }
    
    //计算最大数据条数
    private function mkMaxPage()
    {
        $this->maxPage = ceil($this->maxRows/$this->pageSize);
    }
    
    //校验当前页码
    private function checkPage(){
        if($this->page > $this->maxPage){
            $this->page = $this->maxPage;
        }
        
        if($this->page < 1){
            $this->page = 1;
        }
    }
    
    //输出分页limit子sql语句
    public function limit()
    {
        return (($this->page-1)*$this->pageSize).",".$this->pageSize;
    }
    
    
    //输出分页页码信息
    public function show()
    {
         $url = $_SERVER["PHP_SELF"]; //获取当前页面URL地址
         //继续维持搜索条件
         $params = "";
         //遍历get请求中的参数
         foreach($_GET as $k=>$v){
             if((!empty($v) || $v==='0')&& $k!="p"){
                $params .="&{$k}={$v}";
             }
         }
         // $pre = $this->page-1;
         // $next = $this->page+1;
         // $last = $this->maxPage;
         // $arr = ["pre"=>$pre,"next"=>$next,"first"=>1,"last"=>$last];
         // return $arr;
                         $str = "<div id='page'>
                            <div class='layui-box layui-laypage layui-laypage-default' id='layui-laypage-0'>";

                        $str .= "<a href='{$url}?p=".($this->page-1)."{$params}' class='layui-laypage-next' data-page='{$this->page}'><em>&lt;</em></a>";
                       
                        for ($i=1; $i<=$this->maxPage ; $i++) { 
                             $bg = ($i==$this->page)?"style='background-color:#009688'":"";
                            $str.="<a href='{$url}?p={$i}{$params}' data-page='{$i}' ".$bg.">{$i}</a>";
                        }
                        $str .= "<a  href='{$url}?p=".($this->page+1)."{$params}' class='layui-laypage-next' data-page='{$this->page}'><em>&gt;</em></a>
                    </div>
                </div>";


         // $str = " 当前第{$this->page}/{$this->maxPage}页，共计{$this->maxRows}条 ";
         // $str .=" <a href='{$url}?p=1{$params}'>首页</a> ";
         // $str .=" <a href='{$url}?p=".($this->page-1)."{$params}'>上一页</a> ";
         // $str .=" <a href='{$url}?p=".($this->page+1)."{$params}'>下一页</a> ";
         // $str .=" <a href='{$url}?p={$this->maxPage}{$params}'>尾页</a> ";
         
         return $str;

    }
    
}



/*
$a = [1,2,3,4,5,6,7,8,9,10];
//过滤出6以上的数值及2    [2,7,8,9,10]

foreach($a as $v){
    if($v>6 || $v==2){
        echo $v;   //2,7,8,9,10
    }
}
*/
